const express = require("express");
const controller = require("../controllers/userController");

const userRouter = express.Router();

userRouter.get("/", function (req, res, next) {
    res.send("用户首页");
});

userRouter.post("/register", async (req, res) => {
    const ret = await controller.register(req.body);
    res.json(ret)
});

userRouter.post("/login", async (req, res) => {
    const ret = await controller.login(req.body);
    res.json(ret)
});

/* PUT /user/heige */
userRouter.put(/\w+/, async (req, res) => {
    const users = await controller.getUser({ username: req.path.slice(1) })
    const result = await controller.updateUser(users[0]._id,req.body)
    res.json(result)
});

/* GET /user/heige */
userRouter.get(/\w+/, async (req, res) => {
    const users = await controller.getUser({ username: req.path.slice(1) })
    res.json(users[0])
});

module.exports = userRouter;
